package com.gtw.modules.sys.controller;

import com.gtw.common.annotation.SysLog;
import com.gtw.common.utils.PageUtils;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.modules.entity.TbBanner;
import com.gtw.modules.sys.service.SysBannerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/** Banner Controller
 *
 * @author wuye
 * @date 2018/2/6
 */
@RestController
@RequestMapping("/sys/banner")
public class SysBannerController extends AbstractController{

    @Autowired
    private SysBannerService bannerService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:banner:list")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);

        List<TbBanner> bannerList = bannerService.queryList(query);
        int total = bannerService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(bannerList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:banner:info")
    public R info(@PathVariable("id") Integer id){
        TbBanner banner = bannerService.queryObject(id);

        return R.ok().put("banner", banner);
    }

    /**
     * 保存
     */
    @SysLog("添加banner")
    @RequestMapping("/save")
    @RequiresPermissions("sys:banner:save")
    public R save(@RequestBody TbBanner banner){
        banner.setCreatetime(new Date());
        banner.setCreateuser(getUser().getUsername());
        banner.setUpdatetime(new Date());
        banner.setUpdateuser(getUser().getUsername());
        banner.setIsdelete(0);
        bannerService.save(banner);

        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改banner")
    @RequestMapping("/update")
    @RequiresPermissions("sys:banner:update")
    public R update(@RequestBody TbBanner banner){
        banner.setUpdatetime(new Date());
        banner.setUpdateuser(getUser().getUsername());
        bannerService.update(banner);

        return R.ok();
    }

    /**
     * 删除（删除
     */
    @SysLog("删除banner")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:banner:delete")
    public R delete(@RequestBody Integer[] ids){
        for (int i = 0;i<ids.length;i++){
            TbBanner tbBanner = bannerService.queryObject(ids[i]);
            tbBanner.setIsdelete(1);
            bannerService.update(tbBanner);
        }
        return R.ok();
    }
}
